package top.tangyh.lamp.care.mapper;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import top.tangyh.basic.base.mapper.SuperMapper;
import top.tangyh.lamp.care.entity.BCarePgVte;
import org.springframework.stereotype.Repository;
import top.tangyh.lamp.care.vo.result.Apache2Statstic;
import top.tangyh.lamp.care.vo.result.BCarePgVteResultVO;
import top.tangyh.lamp.care.vo.result.BCarePgVteVO;
import top.tangyh.lamp.care.vo.result.IsbarPgRecordDTO;
import top.tangyh.lamp.common.constant.CommonConstant;
import top.tangyh.lamp.common.utils.PDateUtils;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * Mapper 接口
 * vte风险评分
 * </p>
 *
 * @author zqb
 * @date 2024-07-08 16:40:03
 * @create [2024-07-08 16:40:03] [zqb] [代码生成器生成]
 */
@Repository
public interface BCarePgVteMapper extends SuperMapper<BCarePgVte> {

    default boolean checkPg(String patientIdent, LocalDateTime pfTime, String pfr, Integer type){
        Date start = PDateUtils.stringToDate(PDateUtils.format(new Date(PDateUtils.getTimestampOfDateTime(pfTime)),PDateUtils.DATE_TIME_MINUTE_PATTERN)+":00",PDateUtils.DATE_TIME_PATTERN);
        Date end = PDateUtils.stringToDate(PDateUtils.format(new Date(PDateUtils.getTimestampOfDateTime(pfTime)),PDateUtils.DATE_TIME_MINUTE_PATTERN)+":59",PDateUtils.DATE_TIME_PATTERN);
        LambdaQueryWrapper<BCarePgVte> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BCarePgVte::getPatientIdent,patientIdent)
                .eq(BCarePgVte::getPfr,pfr)
                .eq(BCarePgVte::getIsDeleted,0    )
                .eq(BCarePgVte::getType,type)
                .ge(BCarePgVte::getPfTime,start)
                .le(BCarePgVte::getPfTime,end);
        List<BCarePgVte> olds = selectList(queryWrapper);
        if(CollectionUtil.isNotEmpty(olds)){
            return true;
        }
        else{
            return false;
        }
    }

    List<BCarePgVteVO> getVteOfTopNewByList(@Param("patientIdents")List<String> patientIdents);

    BCarePgVteVO getVteOfTopNew(@Param("patientIdent")String patientIdent);

    default void deletetOld(String patientIdent, Date start, Date end, Set<String> pfr, Set<Integer> type){
        LambdaUpdateWrapper<BCarePgVte> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.set(BCarePgVte::getIsDeleted, CommonConstant.YES_NUMBER);
        queryWrapper.eq(BCarePgVte::getPatientIdent,patientIdent)
                .in(BCarePgVte::getPfr,pfr)
                .in(BCarePgVte::getType,type)
                .ge(BCarePgVte::getPfTime,start)
                .le(BCarePgVte::getPfTime,end);
        update(null,queryWrapper);
    }

    default List<BCarePgVte> selectLatestByParams(String patientIdent, String hospitalNumber, Integer type, Long recordTime,Long rkTime){
        LambdaQueryWrapper<BCarePgVte> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BCarePgVte::getIsDeleted,CommonConstant.NO_NUMBER)
                .eq(BCarePgVte::getPatientIdent,patientIdent).eq(BCarePgVte::getHospitalNumber,hospitalNumber)
                .eq(BCarePgVte::getType,type).le(BCarePgVte::getPfTime,new Date(recordTime));
        if(null != rkTime){
            queryWrapper.ge(BCarePgVte::getPfTime,new Date(rkTime));
        }
        queryWrapper.orderByDesc(BCarePgVte::getPfTime).last(" limit 1 ");
        return selectList(queryWrapper);
    }

    /**
     *  获取最近一条评分--用于isbar交班
     *
     */
    List<IsbarPgRecordDTO> getLatestPgVTERecordAll(@Param("type") int type, @Param("hospitalNumbers") Collection<String> hospitalNumbers, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

    /**
     *  获取最近一条评分--用于isbar交班
     *
     */
    List<IsbarPgRecordDTO> getLatestVTEPgRecord(int type, String hospitalNumber, Date startTime, Date endTime) ;


    /**
     * 入ICU后首次APACHEⅡ评分≥15分的患者
     * @param startTime
     * @param endTime
     * @return
     */
    List<BCarePgVteVO> getFirstApacheiiPatients(String startTime,String endTime);

    /**
     * 自定义分页查询
     * @param page 分页对象
     * @param startTime 查询开始时间
     * @param endTime 查询结束时间
     * @return 分页结果集
     */
    List<BCarePgVteResultVO> secletApacheIIPage(Page<BCarePgVteResultVO> page,
                                                @Param("startTime") String startTime,
                                                @Param("endTime") String endTime);


    List<Apache2Statstic> selectApacheIIFirstStatistics(@Param("startTime") String startTime,
                                                        @Param("endTime") String endTime);
//    IPage<BCarePgVteVO> secletApacheIIPage(IPage<BCarePgVteVO> page,String startTime, String endTime);
}


